package com.indusfo.spc.service.impl;


import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.PalletMapper;
import com.indusfo.spc.mapper.UploadMapper;
import com.indusfo.spc.pojo.Pallet;
import com.indusfo.spc.service.PalletService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 托盘业务层
 *
 * @author chenm
 * @date 2019/07/09
 *
 */
@Service
public class PalletServiceImpl implements PalletService {

	private static final Logger logger = LoggerFactory.getLogger(PalletServiceImpl.class);

	@Resource
	private PalletMapper palletMapper;

	@Resource
	private UploadMapper uploadMapper;

	/**
	 * 新增
	 *
	 * @author chenm
	 * @date 2019/07/09
	 */
	@Override
	public JSONObject insertPallet(Pallet pallet) {
		String remark = pallet.getRemark();
		if (remark == null) {
			remark = "";
		}
		try {
			if(pallet.getPalletId() != null) {
				throw new ParamsErrorException("新增操作不能选择托盘");
			}
			checkParam(pallet, remark);
			checkNoRepeat(pallet);
			int row = palletMapper.insertSelective(pallet);

			if (row == 0) {
				throw new ModifyFailedException("数据新增失败");
			}
			return JSONObject.oK("新增成功");
		} catch (GlobalException e) { // 这里只捕获自定义异常
			logger.error(e.getMessage(), e);
			return JSONObject.build(403, e.getMessage());
		}
	}


	/**
	 * 删除
	 *
	 * @author chenm
	 * @date 2019/07/09 10:44
	 * @param palIds, dataState]
	 * @return com.indusfo.spc.vo.JSONObject
	 */
	@Override
	public JSONObject deletePallet(Integer[] palIds, Integer dataState) {
		try {
			if(dataState == null) {
				throw new ParamsErrorException("数据状态不能为空");
			}else if(dataState != 1 && dataState != 2 && dataState != 3) {
				throw new ParamsErrorException("数据状态错误");
			}
			if (palIds == null) {
				throw new ParamsErrorException("请选择要删除的数据");
			}
			// 执行存储过程
			int row = 0;
			if(dataState == 3){
				row = palletMapper.delPallerts(palIds, dataState,0);
			}else{
				row = palletMapper.delPallerts(palIds, dataState,null);
			}
			String msg = "";
			if (row == 0) {
				// 判断传入的数据状态参数lDataState,返回相应信息
				switch (dataState) {
					case 1:
						throw new ModifyFailedException("启用失败");
					case 2:
						throw new ModifyFailedException("删除失败");
					case 3:
						throw new ModifyFailedException("停用失败");
					default:
				}
			} else {
				switch (dataState) {
					case 1:
						msg = "启用成功";break;
					case 2:
						msg = "删除成功";break;
					case 3:
						msg = "停用成功";break;
					default:
				}
			}
			return JSONObject.oK(msg);
		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(403, e.getMessage());
		}
	}

	/**
	 * 更新托盘信息
	 *
	 * @author chenm
	 * @date 2019/07/09 10:26
	 * @param pallet
	 * @return com.indusfo.spc.vo.JSONObject
	 */
	@Override
	public JSONObject updatePallet(Pallet pallet) {
		String remark = pallet.getRemark();
		if(remark == null) {
			remark = "";
		}
		try {
			if(pallet.getPalletId() == null) {
				throw new ParamsErrorException("请选择要修改的托盘资料");
			}
			if(pallet.getBatchId() ==null){
				pallet.setBatchId(0);
			}
			checkParam(pallet, remark);
			checkNoRepeat(pallet);
			int	row = palletMapper.updateByPrimaryKeySelective(pallet);

			if(row == 0) {
				throw new ModifyFailedException("数据编辑失败");
			}
			return JSONObject.oK("编辑成功");
		} catch (GlobalException e) {  //这里只捕获自定义异常
			logger.error(e.getMessage(), e);
			return JSONObject.build(403, e.getMessage());
		}
	}

	/**
	 * 查询托盘信息
	 *
	 * @author chenm
	 * @date 2019/07/09 10:29
	 * @param pallet
	 * @return com.indusfo.spc.vo.JSONObject
	 */
	@Override
	public JSONObject queryPallet(Pallet pallet) {
		List<Pallet> listPallets = null;
		try {
			Integer pagesize = pallet.getPagesize();
			Integer pageindex = pallet.getPageindex();
			if(pagesize != null && pageindex != null) {
				pallet.setIncept(pagesize*(pageindex - 1));
			}
		  	//查询
			listPallets = palletMapper.listPallets(pallet);
			if (listPallets.isEmpty()) {
                return JSONObject.oK("没有查询到相关数据", listPallets, 0);
            }
			//查询分页总记录数
			int count = palletMapper.countPallerts(pallet);
			return JSONObject.oK("查询成功", listPallets, count);
		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(403, e.getMessage());
		}
	}

	@Override
	public Pallet getByPalCode(String palCode, Integer palId) {
		return palletMapper.getByPalCode(palCode,palId);
	}



	/**
	 * 参数校验
	 *
	 * @author xuz
	 * @date 2018/11/30 2:07 PM
	 * @param pallet, vcRemark]
	 * @return void
	 */
	private void checkParam(Pallet pallet, String remark) {
		if(StringUtils.isEmpty(pallet.getPalletCode())) {
			throw new ParamsErrorException("托盘编号不能为空");
		}
		if (pallet.getPalletCode().getBytes().length > 50) {
			throw new ParamsErrorException("托盘编号超过规定长度,长度50个字节,25个汉字");
		}
		if(StringUtils.isEmpty(pallet.getPalletName())) {
			throw new ParamsErrorException("托盘名称不能为空");
		}
		if (pallet.getPalletName().getBytes().length > 50) {
			throw new ParamsErrorException("托盘名称超过规定长度,长度50个字节,25个汉字");
		}
		if (remark.getBytes().length > 100) {
			throw new ParamsErrorException("备注超过规定长度,长度100个字节,50个汉字");
		}
		if(getByPalCode(pallet.getPalletCode(),pallet.getPalletId()) != null){
			throw new ParamsErrorException("托盘编号已存在");
		}
		if(pallet.getBatchId() != null && pallet.getBatchId() != 0 ){
			// 批次不能重复
			Integer batchCounts = palletMapper.queryBatch(pallet);
			if (batchCounts !=0) {
				throw new ParamsErrorException("该批次已绑定托盘");
			}
		}

	}
	/**
	 * 根据托盘名称去唱
	 */
	public void checkNoRepeat(Pallet pallet){
		int count =palletMapper.selectByPalName(pallet);
		if(count>0){
			throw  new ParamsErrorException("托盘名称已存在");
		}
	}

	@Override
	public String checkNoRepeatExcel(Pallet pallet) {
		if(StringUtils.isBlank(pallet.getPalletName()) && StringUtils.isBlank(pallet.getPalletCode()) ){
			return "该行数据为空(原因:清空行内容时未清空彻底/未输入)";
		}
		if(StringUtils.isEmpty(pallet.getPalletName())) {
			return "托盘名称为空";
		}
		if (pallet.getPalletName().getBytes().length > 50) {
			return "托盘名称超过规定长度,长度50个字节,25个汉字";
		}
		if(StringUtils.isEmpty(pallet.getPalletCode())) {
			return "托盘编号为空";
		}
		if (pallet.getPalletCode().getBytes().length > 50) {
			return "托盘编号超过规定长度,长度50个字节,25个汉字";
		}
		if(getByPalCode(pallet.getPalletCode(),pallet.getPalletId()) != null){
			return "托盘编号已存在";
		}
		int count =palletMapper.selectByPalName(pallet);
		if(count>0){
			return "托盘名称已存在";
		}
		return null;
	}

	@Override
	public JSONObject updateListPallet(List<Pallet> palletsList) {
		int flag = uploadMapper.updateListPallet(palletsList);
		if(flag>0){
			return JSONObject.oK("新增成功");
		}
		return JSONObject.oK("新增失败");
	}

}
